from rest_framework import viewsets
from rest_framework.decorators import action
from system.models import Organization
from system.serializers import OrganizationSerializer
from libs.simple_responses import api_success
from libs.decorators import permission_required
from libs.enums import Permission


class OrganizationViewSet(viewsets.GenericViewSet):
    queryset = Organization.objects.all().order_by('-id')
    serializer_class = OrganizationSerializer
    ordering_fields = ('id',)

    @permission_required([Permission.ORGANIZATION_MANAGE.value])
    @action(methods=['get'], detail=False)
    def info(self, request, *args, **kwargs):
        instance = Organization.objects.first()
        data = OrganizationSerializer(instance).data
        return api_success(data=data)

    @permission_required([Permission.ORGANIZATION_MANAGE.value])
    @action(methods=['post'], detail=False, url_path='edit')
    def edit(self, request, *args, **kwargs):
        data = request.data
        Organization.objects.update(**data)
        instance = Organization.objects.first()
        data = OrganizationSerializer(instance).data
        return api_success(data=data)
